package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.StrongUuidGenerator;
import com.csjy.migration.hnnd.Tool;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目资产表
 * BT_FLOW_FUND_ITEM_EXTENT --》 nd_business_operation
 */
@BusinessDataConverter(
        insertSql = "INSERT INTO ND_BUSINESS_OPERATION(" +
                "ID, ENTERPRISE_ID, FINANCE_PERIOD, TOTAL_ASSET, TOTAL_DEBT, ASSET_RATIO, NET_ASSET, CAPITAL_COLLECTION, NET_ASSET_RATIO, LAST_INCOME, " +
                "LAST_PROFIT, LAST_CASH, LAST_TAX, CURRENT_INCOME, CURRENT_PROFIT, CURRENT_CASH, CURRENT_TAX, CREATE_NAME, CREATE_BY, CREATE_DATE, " +
                "UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, INDUSTRY) VALUES " +
                "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " +
                "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " +
                "?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE  ND_BUSINESS_OPERATION  SET  ENTERPRISE_ID  = ?,  FINANCE_PERIOD  = ?,  TOTAL_ASSET  = ?,  TOTAL_DEBT  = ?,  ASSET_RATIO  = ?, " +
        " NET_ASSET  = ?,  CAPITAL_COLLECTION  = ?,  NET_ASSET_RATIO  = ?,  LAST_INCOME  = ?,  LAST_PROFIT  = ?,  LAST_CASH  = ?,  " +
        "LAST_TAX  = ?,  CURRENT_INCOME  = ?,  CURRENT_PROFIT  = ?,  CURRENT_CASH  = ?,  CURRENT_TAX  = ?,  " +
        "CREATE_NAME  = ?,  CREATE_BY  = ?,  CREATE_DATE  = ?,  UPDATE_NAME  = ?,  UPDATE_BY  = ?,  UPDATE_DATE  = ?,  SYS_ORG_CODE  = ?, " +
        " SYS_COMPANY_CODE  = ?,  BPM_STATUS  = ?,  INDUSTRY  = ? WHERE  ID  = ?",
        findMigratedIdListSql = "SELECT t1.id FROM ND_BUSINESS_OPERATION t1 LEFT JOIN ND_PROJECT_INFO t2 ON t1.ENTERPRISE_ID = t2.CUSTOMER_ID WHERE t2.id = ? ",
        seq = 209
)
public class NdBusinessOperation extends AbstractBusiness {

    public NdBusinessOperation(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }


    @Override
    public void migrate()  throws Exception {

        // 旧系统数据
        List<Map> btFlowFundItemExtentList = this.sourceData.getBtFlowFundItemExtent();

        // 筛选数据
        for (Map btFlowFundItemExtent : btFlowFundItemExtentList){
            if(btFlowFundItemExtent.get("IS_LATEST").toString().equals("0")) {
                continue;
            }
            // 判断是否需要覆盖迁移
            int needMigratedResult = this.needMigrated(buildId(btFlowFundItemExtent));
            if(needMigratedResult == 0) {
                continue;
            }
            // 单条插入或更新
            if(needMigratedResult == 1) {
            this.targetDbUtil.updateByParams(updateSql,this.buildValues(btFlowFundItemExtent),true);
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchUpdateValues.add(this.buildValues(btFlowFundItemExtent));
            } else {
            this.targetDbUtil.updateByParams(insertSql,this.buildValues(btFlowFundItemExtent));
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchInsertValues.add(this.buildValues(btFlowFundItemExtent));
            }

        }
    }


    public String buildId(Map btFlowFundItemExtent){
        return this.sourceData.getBtItemBasic().get(0).get("ENTERPRISENO").toString();
    }

    public List buildValues(Map btFlowFundItemExtent){
        List<Object> result= new ArrayList<>();
        String id =  buildId(btFlowFundItemExtent);
        //        ID
        result.add(id);
        //        ENTERPRISE_ID	企业客户id
        result.add(buildId(btFlowFundItemExtent));
        //        FINANCE_PERIOD	财务报告期
        result.add(btFlowFundItemExtent.get("CURRENTLYMONTH"));
        //        TOTAL_ASSET	资产总额|万
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("CURRENTLYASSETTOTAL")));
        //        TOTAL_DEBT	负债总额|万
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("CURRENYLYOWESTOTAL")));
        //        ASSET_RATIO	资产负债率%
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("ASSETOWESRATE")));
        //        NET_ASSET	有效净资产|万
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("EFFICIENCYASSET")));
        //        CAPITAL_COLLECTION	实收资本|万
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("REALGAINCAPITAL")));
        //        NET_ASSET_RATIO	担保金额占有效净资产比率%
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("ASSUREEFFICIENCYRATE")));
        //        LAST_INCOME	上年营业收入
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("LASTYEARSELLTOTAL")));
        //        LAST_PROFIT	净利润额
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("LASTYEARPROFIT")));
        //        LAST_CASH	经营活动净现金流量
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("LASTYEARFARECASHFLOW")));
        //        LAST_TAX	纳税额
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("LASTYEARINTERESTRATE")));
        //        CURRENT_INCOME	本期营业收入
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("THISYEARSELLTOTAL")));
        //        CURRENT_PROFIT	净利润额
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("THISYEARPROFIT")));
        //        CURRENT_CASH	经营活动净现金流量
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("THISYEARSFARECASHFLOW")));
        //        CURRENT_TAX	纳税额
        result.add(Tool.covertToBigDecimal(btFlowFundItemExtent.get("THISYEARINTERESTRATE")));
        //        CREATE_NAME	创建人名称
        result.add(null);
        //        CREATE_BY	创建人登录名称
        result.add(null);
        //        CREATE_DATE	创建日期
        result.add(btFlowFundItemExtent.get("CREATE_TIME"));
        //        UPDATE_NAME	更新人名称
        result.add(null);
        //        UPDATE_BY	更新人登录名称
        result.add(null);
        //        UPDATE_DATE	更新日期
        result.add(btFlowFundItemExtent.get("MODIFY_TIME"));
        //        SYS_ORG_CODE	所属部门
        result.add(null);
        //        SYS_COMPANY_CODE	所属公司
        result.add("002");
        //        BPM_STATUS	流程状态
        result.add("1");
        //        INDUSTRY
        result.add(null);

        return result;
    }

}
